# 使用 StrictRedis 连接池
import redis
import json

import configparser

cf = configparser.ConfigParser()
cf.read("config/application.ini")

# 解析redis配置信息
def parseRedisInfo():
    redis_host = cf.get("redis","redis_host")
    redis_port = cf.get("redis","redis_port")
    redisInfoDict = {"redis_host": redis_host, "redis_port": redis_port}
    return redisInfoDict

# 初始化redis
pool = redis.ConnectionPool(host=parseRedisInfo()['redis_host'], port=parseRedisInfo()['redis_port'], decode_responses=True)
redisUtil = redis.StrictRedis(connection_pool=pool)

# 保存问题URL至redis
def saveProblemUrlListToRedis(visaUrlList,shipUrlList):
    redisUtil.delete("visaProblemUrlList")
    redisUtil.delete("shipProblemUrlList")
    for visaUrl in visaUrlList:
        redisUtil.lpush('visaProblemUrlList',visaUrl)
    for shipUrl in shipUrlList:
        redisUtil.lpush('shipProblemUrlList',shipUrl)

def getShipProblemUrlListFromRedis():
    shipUrlList = redisUtil.lrange('shipProblemUrlList', 0, -1)
    shipUrlObjList = []
    for shipUrl in shipUrlList:
        json_obj = json.loads(shipUrl.replace("'", "\""))
        shipUrlObjList.append(json_obj)
    return shipUrlObjList
    return redisUtil.lrange('shipProblemUrlList',0,-1)

def getVisaProblemUrlListFromRedis():
    visaUrlList = redisUtil.lrange('visaProblemUrlList', 0, -1)
    visaUrlObjList = []
    for visaUrl in visaUrlList:
        json_obj = json.loads(visaUrl.replace("'", "\""))
        visaUrlObjList.append(json_obj)
    return visaUrlObjList

def initRedisUtils():
    return redisUtil
